// 导入数据库
const db = require('../db/index')
/*  product表
* id int
* product_id 入库编号 int
* product_name 产品名称 varchar
* product_category 产品类别  varchar
* product_unit 产品单位 varchar
* product_inwarehouse_number 产品入库数量 库存 int
* product_single_price 产品入库单价 int
* product_all_price 产品入库总价 int
* product_status 库存状态 100 - 300为正常 100以下为库存告急 300以上为过剩 varchar
* product_create_person 入库操作人 varchar
* product_create_time 产品新建时间 varchar
* product_update_time 产品最新编辑时间 varchar
* in_memory 入库备注
*
* product_out_id 出库id int
* product_out_number 出库数量 int
* product_out_price 出库总价 int
* product_out_apply_person 出库申请人 varchar
* product_apply_time 申请出库时间 varchar
* apply_memory 申请备注 varchar
* product_out_status 出库状态 申请出库 or 同意 or 否决 varchar
* product_audit_time 审核时间 varchar
* product_out_audit_person 审核人 varchar
* audit_memory 出库 / 审核备注 varchar
*/

// 产品入库/创建产品
exports.createProduct = (req, res) => {
    const { product_id, product_name, product_category, product_unit, product_inwarehouse_number,
        product_single_price, product_create_person, in_memory
    } = req.body
    const sql1 = "select * from product where product_id = ?"
    db.query(sql1, product_id, (err, result) => {
        if (err) return res.cc(err)
        if (result.length > 0) return res.send({ status: 1, message: '入库编号已存在，请重新输入' })
        // 记录入库时间
        const product_create_time = new Date()
        // 计算入库总价
        const product_all_price = product_inwarehouse_number * product_single_price
        const sql2 = "insert into product set ?"
        db.query(sql2, {
            product_id, product_name, product_category, product_unit,
            product_inwarehouse_number, product_single_price, product_all_price,
            product_create_person, product_create_time, in_memory
        }, (err, result) => {
            if (err) return res.cc(err)
            res.send({
                status: 0,
                message: '创建产品成功'
            })
        })
    })
}

// 编辑产品信息
exports.editProduct = (req, res) => {
    const { id, product_name, product_category, product_unit,
        product_inwarehouse_number, product_single_price, in_memory
    } = req.body
    // 记录更新时间
    const product_update_time = new Date()
    // 计算变化后的总价
    const product_all_price = product_inwarehouse_number * product_single_price
    const sql1 = "update product set ? where id = ?"
    db.query(sql1, [{
        id, product_name, product_category, product_unit,
        product_inwarehouse_number, product_single_price, product_all_price,
        product_update_time, in_memory
    }, id], (err, result) => {
        if (err) return res.cc(err)
        res.send({
            status: 0,
            message: '编辑产品成功'
        })
    })
}

// 删除产品 id =>
exports.deleteProduct = (req, res) => {
    const { id } = req.body
    const sql1 = "delete from product where id = ?"
    db.query(sql1, id, (err, result) => {
        if (err) return res.cc(err)
        res.send({
            status: 0,
            message: '删除产品成功'
        })
    }
    )
}

// 获取产品列表 无 =>
exports.getProductList = (req, res) => {
    // 获取库存数量大于0的所有产品信息列表
    const sql1 = "select * from product where product_inwarehouse_number > 0"
    db.query(sql1, (err, result) => {
        if (err) return res.cc(err)
        res.send(result)
    })
}

// 产品申请出库
exports.applyOutProduct = (req, res) => {
    const product_out_status = '申请出库'
    const { id, product_out_id, product_out_number, product_single_price,
        product_out_apply_person, apply_memory
    } = req.body
    // 在两张表中查找是否存在相同的出库编号，存在就返回前端失败
    const sql1 = "select * from product where product_out_id = ?"
    db.query(sql1, product_out_id, (err, result) => {
        if (err) return res.cc(err)
        if (result.length > 0) return res.send({ status: 1, message: '出库编号存在，请输入新的出库编号' })
        const sql2 = "select * from outproduct where product_out_id = ?"
        db.query(sql2, product_out_id, (err, result) => {
            if (err) return res.cc(err)
            if (result.length > 0) return res.send({ status: 1, message: '出库编号存在，请输入新的出库编号' })
            // 记录申请时间
            const product_out_apply_time = new Date()
            // 计算申请出库总价
            const product_out_price = product_out_number * product_single_price
            const sql3 = "update product set ? where id = ?"
            db.query(sql3, [{
                product_out_id, product_out_number, product_out_price, product_out_apply_person,
                product_out_apply_time, apply_memory, product_out_status
            }, id], (err, result) => {
                if (err) return res.cc(err)
                res.send({
                    status: 0,
                    message: '产品申请出库成功'
                })
            })
        })
    })
}

// 产品再次申请出库
exports.applyOutProductAgain = (req, res) => {
    const product_out_status = '申请出库'
    const { id, product_out_id, product_out_number, product_single_price,
        product_out_apply_person, apply_memory
    } = req.body
    const product_out_apply_time = new Date()
    // 计算申请出库总价
    const product_out_price = product_out_number * product_single_price
    const sql3 = "update product set ? where id = ?"
    db.query(sql3, [{
        product_out_id, product_out_number, product_out_price, product_out_apply_person,
        product_out_apply_time, apply_memory, product_out_status
    }, id], (err, result) => {
        if (err) return res.cc(err)
        res.send({
            status: 0,
            message: '产品申请出库成功'
        })
    })
}

// 获取产品出库申请列表 无 =>
exports.getApplyOutList = (req, res) => {
    // product_out_status为申请出库或者否决都可以出现在出库审核列表中
    const sql1 = "select * from product where product_out_status != '同意'"
    db.query(sql1, (err, result) => {
        if (err) return res.cc(err)
        res.send(result)
    })
}

// 审核员产品审核
exports.auditOutProduct = (req, res) => {
    const { id, product_out_id, product_out_number, product_single_price, product_name, product_out_apply_time,
        product_out_apply_person, product_out_status, audit_memory, product_out_audit_person, apply_memory
    } = req.body
    let { product_inwarehouse_number } = req.body
    // 记录审核时间
    const product_out_audit_time = new Date()
    if (product_out_status === '同意') {
        // 新的库存数量
        product_inwarehouse_number = product_inwarehouse_number - product_out_number
        // 计算出库总价
        const product_out_price = product_out_number * product_single_price
        // 计算新的库存总价
        const product_all_price = product_inwarehouse_number * product_single_price
        const sql1 = "insert into outproduct set ?"
        db.query(sql1, {
            product_out_id, product_name, product_out_number, product_out_price,
            product_out_apply_person, product_out_audit_person, product_out_apply_time,
            product_out_audit_time, apply_memory, audit_memory
        }, (err, result) => {
            if (err) return res.cc(err)
            const sql2 = "update product set ? where id = ?"
            const product_update_time = new Date()
            db.query(sql2, [{
                product_out_id: null, product_out_number: null, product_out_price: null,
                product_out_apply_person: null, product_out_apply_time: null, apply_memory: null,
                product_out_status: null, product_out_audit_time: null, product_out_audit_person: null,
                audit_memory: null, product_inwarehouse_number, product_all_price, product_update_time,
            }, id], (err, result) => {
                if (err) return res.cc(err)
                res.send({
                    status: 0,
                    message: '出库同意成功'
                })
            })
        })
    } else if (product_out_status === '否决') {
        const sql3 = "update product set ? where id = ?"
        db.query(sql3, [{ audit_memory, product_out_status, product_out_audit_time, product_out_audit_person }, id], (err, result) => {
            if (err) return res.cc(err)
            res.send({
                status: 0,
                message: '出库否决成功'
            })
        })
    }
}

// 申请人撤回申请 id =>
exports.withdrawApplyOut = (req, res) => {
    const { id } = req.body
    const sql1 = "update product set ? where id = ?"
    db.query(sql1, [{
        product_out_id: null, product_out_number: null, product_out_price: null,
        product_out_apply_person: null, product_out_apply_time: null, apply_memory: null,
        product_out_status: null, product_out_audit_person: null, product_out_audit_time: null, audit_memory: null
    }, id], (err, result) => {
        if (err) return res.cc(err)
        res.send({
            status: 0,
            message: '撤回申请出库成功'
        })
    })
}

// 通过入库编号搜索产品 product_id =>
exports.searchProductById = (req, res) => {
    const { product_id } = req.body
    const sql1 = "select * from product where product_id = ?"
    db.query(sql1, product_id, (err, result) => {
        if (err) return res.cc(err)
        res.send(result)
    })
}

// 通过出库编号搜索产品 product_out_id =>
exports.searchProductByOutId = (req, res) => {
    const { product_out_id } = req.body
    const sql1 = "select * from product where product_out_id = ?"
    db.query(sql1, product_out_id, (err, result) => {
        if (err) return res.cc(err)
        res.send(result)
    })
}

// 通过出库编号搜索出库产品 product_out_id =>
exports.searchOutProductByOutId = (req, res) => {
    const { product_out_id } = req.body
    const sql1 = "select * from outproduct where product_out_id = ?"
    db.query(sql1, product_out_id, (err, result) => {
        if (err) return res.cc(err)
        res.send(result)
    })
}

// 分页实现=============================
// 获取库存产品总数 无 =>
exports.getProductNumber = (req, res) => {
    const sql1 = "select * from product where product_inwarehouse_number > 0"
    db.query(sql1, (err, result) => {
        if (err) return res.cc(err)
        res.send({
            length: result.length
        })
    })
}
// 获取申请出库产品总数 无 =>
exports.getApplyNumber = (req, res) => {
    const sql1 = "select * from product where product_out_status is not null"
    db.query(sql1, (err, result) => {
        if (err) return res.cc(err)
        res.send({
            length: result.length
        })
    })
}
// 获取出库产品总数 无 =>
exports.getOutNumber = (req, res) => {
    const sql1 = "select * from outproduct"
    db.query(sql1, (err, result) => {
        if (err) return res.cc(err)
        res.send({
            length: result.length
        })
    })
}
// 库存产品列表监听换页 页码pager =>
exports.returnProductData = (req, res) => {
    // 传来的页码
    const { pager } = req.body
    const number = (pager - 1) * 10
    // 一页10个数据
    // limit 10 offset ${number} 表示从第number个起取10个
    const sql1 = `select * from product order by product_create_time limit 10 offset ${number}`
    db.query(sql1, (err, result) => {
        if (err) {
            return res.cc(err)
        }
        res.send(result)
    })
}
// 申请产品列表监听换页 页码pager =>
exports.returnApplyData = (req, res) => {
    // 传来的页码
    const { pager } = req.body
    const number = (pager - 1) * 10
    // 一页10个数据
    // limit 10 offset ${number} 表示从第number个起取10个
    const sql1 = `select * from product where product_out_status is not null order by product_create_time limit 10 offset ${number}`
    db.query(sql1, (err, result) => {
        if (err) {
            return res.cc(err)
        }
        res.send(result)
    })
}
// 出库产品列表监听换页 页码pager =>
exports.returnOutData = (req, res) => {
    // 传来的页码
    const { pager } = req.body
    const number = (pager - 1) * 10
    // 一页10个数据
    // limit 10 offset ${number} 表示从第number个起取10个
    const sql1 = `select * from outproduct order by product_out_audit_time limit 10 offset ${number}`
    db.query(sql1, (err, result) => {
        if (err) {
            return res.cc(err)
        }
        res.send(result)
    })
}
